package com.example.demo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.demo.entity.Entity;

public class Login2Activity extends AppCompatActivity {

    private Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        Context context = Login2Activity.this;

        TextView tvLogin = findViewById(R.id.btn_login);
        tvLogin.setEnabled(false);

        EditText edtUsername = findViewById(R.id.edit_username);
        EditText edtPassword = findViewById(R.id.edit_pwd);



        edtUsername.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(edtUsername.getText().toString())){
                    tvLogin.setEnabled(false);
                }
                else {
                    tvLogin.setEnabled(true);
                }
            }
        });

        findViewById(R.id.tv_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Login2Activity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        TextView view = findViewById(R.id.btn_login);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (TextUtils.isEmpty(edtUsername.getText().toString())){
                    Toast.makeText(context, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(edtPassword.getText().toString())){
                    Toast.makeText(context, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }



                //跳转到另一个Activity
                //Context context = Login2Activity.this; context 上下文对象 /
                Intent intent = new Intent(context, SettingActivity2.class);

                // activity 跳转传递数据
                intent.putExtra("username", edtUsername.getText().toString());
                intent.putExtra("password", edtPassword.getText().toString());

                Entity entity = new Entity("1", "1");

                //startActivity(new Intent(intent));

                startActivityForResult(intent, 1);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK)
        {
            if (requestCode == 1)
            {
                String result = data.getStringExtra("Result");

            }
        }

    }

}